package cn.donting.jme.expansion.inspect;

import cn.donting.jme.EditorContent;
import cn.donting.jme.event.NameChangeEvent;
import cn.donting.jme.expansion.attribute.Attribute;
import cn.donting.jme.expansion.attribute.BaseInfoAttributeGroup;
import cn.donting.jme.expansion.attribute.TitlePaneAttributeGroup;
import cn.donting.jme.expansion.attribute.ValueChangeListener;
import cn.donting.jme.expansion.attribute.lmpl.StringAttribute;
import cn.donting.jme.expansion.builder.light.LightAttributeBuilder;
import cn.donting.jme.inspect.Inspect;
import cn.donting.jme.ioc.Bean;
import com.jme3.light.Light;
import com.jme3.light.LightList;
import com.jme3.scene.Spatial;

import java.util.ArrayList;
import java.util.List;

@Bean
public class LightInspectBuilder implements InspectBuilder<Light> {
    @Override
    public boolean isHandle(Object o) {
        return o instanceof Light;
    }


    public TitlePaneAttributeGroup create(Light light, BaseInfoAttributeGroup baseInfoAttributeGroup) {
        List<Attribute> attributes = new ArrayList<>();
        LightAttributeBuilder lightBuilder = null;
        List<LightAttributeBuilder> lightAttributeBuilders =
                EditorContent.getIocContainer().getBeansByTypes(LightAttributeBuilder.class);
        TitlePaneAttributeGroup titlePaneAttributeGroup = new TitlePaneAttributeGroup(light.getType().name());
        for (LightAttributeBuilder lightAttributeBuilder : lightAttributeBuilders) {
            if (lightAttributeBuilder.isHandle(light)) {
                lightAttributeBuilder.attribute(attributes, light);
                if (light.isEnabled()) {
                    titlePaneAttributeGroup.setTitleIcon(lightAttributeBuilder.getActiveIcon(LightAttributeBuilder.ICON_SIZE));
                } else {
                    titlePaneAttributeGroup.setTitleIcon(lightAttributeBuilder.getIcon(LightAttributeBuilder.ICON_SIZE));
                }
                baseInfoAttributeGroup.setIcon(lightAttributeBuilder.getIcon(BaseInfoAttributeGroup.ICON_SIZE));
                lightBuilder = lightAttributeBuilder;
            }
        }
        final LightAttributeBuilder finalLightBuilder = lightBuilder;
        titlePaneAttributeGroup.setTitle(getLightTitle(finalLightBuilder, light));

        titlePaneAttributeGroup.setEnabledValueChange(newValue -> {
            light.setEnabled(newValue);
            if (finalLightBuilder != null && light.isEnabled()) {
                titlePaneAttributeGroup.setTitleIcon(finalLightBuilder.getActiveIcon(LightAttributeBuilder.ICON_SIZE));
            } else {
                titlePaneAttributeGroup.setTitleIcon(finalLightBuilder.getIcon(LightAttributeBuilder.ICON_SIZE));
            }
        });

        titlePaneAttributeGroup.setEnabled(light.isEnabled());

        titlePaneAttributeGroup.addAttribute(attributes);
        titlePaneAttributeGroup.setExpand(true);
        return titlePaneAttributeGroup;
    }

    @Override
    public void builder(Inspect inspect, Light light) {

        BaseInfoAttributeGroup baseInfoAttributeGroup = new BaseInfoAttributeGroup();
        baseInfoAttributeGroup.setEnabledCheckBox(true);
        baseInfoAttributeGroup.setEnabledCheckBoxSelect(light.isEnabled());
        baseInfoAttributeGroup.setEnabledChange(newValue -> {
            light.setEnabled(newValue);
            NameChangeEvent nameChangeEvent = new NameChangeEvent(this, light);
            EditorContent.postEvent(nameChangeEvent);
        });

        baseInfoAttributeGroup.setType(light.getClass().getName());
        baseInfoAttributeGroup.setName(light.getName());
        baseInfoAttributeGroup.setNameChange(newValue -> {
            light.setName(newValue);
            NameChangeEvent nameChangeEvent = new NameChangeEvent(this, light);
            EditorContent.postEvent(nameChangeEvent);
        });

        TitlePaneAttributeGroup titlePaneAttributeGroup = create(light, baseInfoAttributeGroup);

        inspect.setTop(baseInfoAttributeGroup);
        inspect.addAttributeGroup(titlePaneAttributeGroup);
    }

    private String getLightTitle(LightAttributeBuilder lightBuilder, Light light) {
        if (lightBuilder == null) {
            return light.getType().name() + "(" + light.getName() + ")";
        }
        return lightBuilder.getName() + "(" + light.getName() + ")";
    }


}
